package com.itheima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.mapper.ApArticleConfigMapper;
import com.itheima.article.mapper.ApArticleContentMapper;
import com.itheima.article.mapper.ApArticleMapper;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.pojo.ApArticleConfig;
import com.itheima.article.pojo.ApArticleContent;
import com.itheima.article.service.ApArticleService;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.pojo.PageRequestDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-08-20
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {



    @Autowired
    private ApArticleMapper articleMapper;
    @Autowired
    private ApArticleConfigMapper articleConfigMapper;
    @Autowired
    private ApArticleContentMapper articleContentMapper;

    /**
     * 添加文章(更新)
     * @param articleInfoDto
     * @return
     */
    @Override
    public ApArticle saveArticle(ArticleInfoDto articleInfoDto) {
        //判断文章是否存在
        ApArticle apArticle = articleInfoDto.getApArticle();
        if (apArticle.getId() == null) {
            //添加
           apArticle.setCreatedTime(LocalDateTime.now());
            articleMapper.insert(apArticle);

            ApArticleConfig apArticleConfig = articleInfoDto.getApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            articleConfigMapper.insert(apArticleConfig);

            ApArticleContent apArticleContent = articleInfoDto.getApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            articleContentMapper.insert(apArticleContent);

        }else {
            //更新
            articleMapper.updateById(apArticle);
            //文本修改
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("article_id",apArticle.getId());
            articleContentMapper.update(articleInfoDto.getApArticleContent(),queryWrapper);


        }


        return apArticle;
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageInfo<ApArticle> searchOrder(PageRequestDto<ApArticle> dto) {
        //查询总数
        Long total = articleMapper.findCount(dto.getBody().getChannelId());
        //分页查询集合信息
        List<ApArticle> articles = articleMapper.findList(dto);

        Long totalPages = total%dto.getSize() == 0?total/dto.getSize() :total/dto.getSize()+1;
        PageInfo<ApArticle> pageInfo = new PageInfo<>(dto.getPage(),dto.getSize(),total,totalPages,articles);

        return pageInfo;


    }

    /**
     * 查看文章详情
     * @param id
     * @return
     */
    @Override
    public ArticleInfoDto detailByArticleId(Long id) {
        ApArticle apArticle = articleMapper.selectById(id);

        ApArticleConfig apArticleConfig = articleConfigMapper.seleteByArticleId(id);

        ApArticleContent apArticleContent = articleContentMapper.selectByArticleId(id);

        ArticleInfoDto dto = new ArticleInfoDto();
        dto.setApArticle(apArticle);
        dto.setApArticleContent(apArticleContent);
        dto.setApArticleConfig(apArticleConfig);


        return dto;
    }
}




















